package com.hs.pre.module.perinfo;


import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hs.HsBase;
import com.hs.common.JSONHandle;
import com.hs.core.db.DBOperator;
import com.hs.core.db.DatabaseConnection;
import com.hs.core.db.HsPreparedStatement;
import com.hs.pre.model.PreEntry;
import com.hs.pre.page.PageImpl;
import com.hs.pre.page.PageParam;
import com.hs.pre.page.PageQuery;
import com.hs.pre.page.PageUtil;
import com.hs.pre.page.ReverseLoadData;


public class PerInfoDAO {

	/**
	 * 通过分类查处方剂
	 * @param sortId
	 * @return
	 */
	public PageUtil  getPerInfoList(Integer sortId ,PageParam pageParam )
	{
//		DBOperator dbo = null;
//		try {
//			dbo = DatabaseConnection.getDBOperator();
//		} catch (Exception e) { 
//		}
//		if(dbo == null)
//			return null;
//		List  listData = new ArrayList(); 
//		String sql = (String) HsBase.getAnsiSqlMap().get("preinfo.select");
//		List list = dbo.select(sql, new Object[]{sortId});
//		Iterator iterator = list.iterator();
//		
//		while(iterator.hasNext())
//		{
//			Map row = (Map)iterator.next();
//			listData.add(new PreEntry(row));
//		}
//		
//		return listData
		String sql = (String) HsBase.getAnsiSqlMap().get("preinfo.select");
	
		try {
			PageQuery a= new PageQuery(sql,new Object[]{sortId},pageParam,new ReversetData());
			return a.getPageUtil();
		} catch (Exception e) { 
			e.printStackTrace();
		}
		
		return null;
	}
	public PageUtil  getNotSortPerInfoList(PageParam pageParam )
	{
		String sql = (String) HsBase.getAnsiSqlMap().get("preinfo.select_notSort");
		try {
			PageQuery a= new PageQuery(sql,null,pageParam,new ReversetData());
			return a.getPageUtil();
		} catch (Exception e) { 
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 获得分类列表
	 * @param sortId
	 * @return
	 */
	public PageUtil  getPreList(PageParam pageParam )
	{ 
		String sql = (String) HsBase.getAnsiSqlMap().get("preinfo.select_all");
		try {
			PageQuery a= new PageQuery(sql,null,pageParam,new ReversetData());
			return a.getPageUtil();
		} catch (Exception e) { 
			e.printStackTrace();
		}
		
		return null;
	}
//	pre_sort_relation
	
	/**
	 * 把方剂添加到分类中
	 */
	public void addPreSort(String va,String sortId)
	{
		String sql = (String) HsBase.getAnsiSqlMap().get("preinfo.insert_notSort");
		String sqlin = "select * from  app.pre_info where ID in("+va+")";
		//(String) HsBase.getAnsiSqlMap().get("preinfo.select_id");
		DBOperator dbo = null;
		try {
			
			dbo = DatabaseConnection.getDBOperator();
			List list = dbo.select(sqlin, null);
			Iterator iterator = list.iterator();
			List v = new ArrayList();
			while(iterator.hasNext())
			{
				Map map =(Map)iterator.next();
				v.add(new Object[]{(Long)map.get("PREID"),sortId});
			}
		HsPreparedStatement st = new HsPreparedStatement(sql); 
		st.executeUpdate(v);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 解除方剂与分类关系
	 * @param preId 方剂id
	 * @param sortId 分类id
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	public void delSortPreRel(List list) throws NumberFormatException, Exception
	{
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_sort_relation.delete");
		HsPreparedStatement st = new HsPreparedStatement(sql);
		st.executeUpdate(list);

	}
	
	class ReversetData implements ReverseLoadData
	{

		public Object load(Map map) {
			// TODO Auto-generated method stub
			return new PreEntry(map);
		}
		
		
	}
	
	/**
	 *  获得分类目录下所有子分类的方剂
	 * @param sortId
	 * @param pageParam
	 * @return
	 */
	public PageUtil  getPerInfoList(String sortId ,PageParam pageParam )
	{
		String sql = (String) HsBase.getAnsiSqlMap().get("preinfo.select");
	
		try {
			PageQuery a= new PageQuery(sql,new Object[]{sortId},pageParam,new ReversetData());
			return a.getPageUtil();
		} catch (Exception e) { 
			e.printStackTrace();
		}
		
		return null;
	}
}
